#include "common/type/data_type.h"
#include "common/value.h"
#include "gtest/gtest.h"

TEST(DateTypeTest, parse_and_to_string_valid)
{
  Value v;
  auto dt = DataType::type_instance(AttrType::DATES);
  ASSERT_EQ(dt->set_value_from_str(v, "1970-01-01"), RC::SUCCESS);
  EXPECT_EQ(v.attr_type(), AttrType::DATES);
  std::string s;
  ASSERT_EQ(dt->to_string(v, s), RC::SUCCESS);
  EXPECT_EQ(s, "1970-01-01");

  Value v2;
  ASSERT_EQ(dt->set_value_from_str(v2, "2024-02-29"), RC::SUCCESS);
  ASSERT_EQ(dt->to_string(v2, s), RC::SUCCESS);
  EXPECT_EQ(s, "2024-02-29");

  Value v3;
  ASSERT_EQ(dt->set_value_from_str(v3, "1800-01-01"), RC::SUCCESS);
  ASSERT_EQ(dt->to_string(v3, s), RC::SUCCESS);
  EXPECT_EQ(s, "1800-01-01");
}

TEST(DateTypeTest, parse_invalid)
{
  Value v;
  auto dt = DataType::type_instance(AttrType::DATES);
  EXPECT_NE(dt->set_value_from_str(v, "2023-02-29"), RC::SUCCESS);
  EXPECT_NE(dt->set_value_from_str(v, "2024-13-01"), RC::SUCCESS);
  EXPECT_NE(dt->set_value_from_str(v, "2024-04-31"), RC::SUCCESS);
  EXPECT_NE(dt->set_value_from_str(v, "abc"), RC::SUCCESS);
}

TEST(DateTypeTest, comparison)
{
  Value a, b;
  auto dt = DataType::type_instance(AttrType::DATES);
  ASSERT_EQ(dt->set_value_from_str(a, "2020-01-01"), RC::SUCCESS);
  ASSERT_EQ(dt->set_value_from_str(b, "2020-01-02"), RC::SUCCESS);
  EXPECT_LT(a.compare(b), 0);
  EXPECT_GT(b.compare(a), 0);
  Value c;
  ASSERT_EQ(dt->set_value_from_str(c, "2020-01-02"), RC::SUCCESS);
  EXPECT_EQ(b.compare(c), 0);
}
